﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using CccNh.Sorting;
using urn.nhibernate.mapping.Item2.Item2;

namespace CccNh.Conventions
{
    public class CreateBasicProperty : IClassConvention, IRunAfter<CreateHiloIdIfTypeHasIntIdField>, IRunAfter<CreateHiloIdIfTypeHasIntIdProperty>
    {
        private readonly IList<Type> basicTypes = new[] { typeof(Guid), typeof(Guid?), typeof(int), typeof(int?), typeof(string), typeof(bool), typeof(bool?), typeof(DateTime), typeof(DateTime?) }.ToList().AsReadOnly();

        public void Apply(Type type, @class @class, IEnumerable<Type> entityTypes, hibernatemapping mapping)
        {
            var properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                .Where(p => p.Name != @class.id.name)
                .Where(p => basicTypes.Contains(p.PropertyType));
            foreach (var propertyInfo in properties)
            {
                @class.property.Add(new property()
                {
                    name = propertyInfo.Name,
                });
            }
        }
    }
}